using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node enum_specifier in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnenum_specifier : IFullFormatter {
        // [120]=enum_specifier : 'enum' '{' enumerator_list '}' ;
        // [121]=enum_specifier : 'enum' 'identifier' '{' enumerator_list '}' ;
        // [122]=enum_specifier : 'enum' 'identifier' ;

        private readonly TokenRange _scope;
        private readonly Token r4;
        private readonly Token? r3;
        private readonly Token? r2;
        private readonly Vnenumerator_list? r1;
        private readonly Token? r0;

        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            context.PrintBlanksAnd(r4, preConfig, writer);
            var config = new BlankConfig();
            if (r3 != null) {
                context.PrintCommentsBetween(r4, r3, config, writer);
                context.PrintBlanksAnd(r3, config, writer);
            }
            if (r2 != null && r1 != null && r0 != null) {
                if (r3 != null) { context.PrintCommentsBetween(r3, r2, config, writer); }
                else { context.PrintCommentsBetween(r4, r2, config, writer); }
                context.PrintBlanksAnd(r2, config, writer);
                context.PrintCommentsBetween(r2, r1, config, writer);
                context.PrintBlanksAnd(r1, config, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        // [121]=enum_specifier : 'enum' 'identifier' '{' enumerator_list '}' ;
        // dumped by DefaultExtractor
        internal Vnenum_specifier(Token r4, Token? r3, Token? r2, Vnenumerator_list? r1, Token? r0) {
            this.r4 = r4;
            this.r3 = r3;
            this.r2 = r2;
            this.r1 = r1;
            this.r0 = r0;
            if (r0 != null) { this._scope = new TokenRange(r4, r0); }
            else if (r1 != null) { this._scope = new TokenRange(r4, r1); }
            else { throw new NotImplementedException(); }
        }
    }
}
